package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.databinding.ActivityUserInfoCodeBinding;
import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.SpUtils;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.chuanjiabao.wisdom.util.timeutil.GetCodeTimeUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class UserInfoCodeActivity extends BaseActivity {

    ActivityUserInfoCodeBinding binding;
    GetCodeTimeUtil timeUtil;
    String phone, code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_user_info_code);
        binding.setActivity(this);
        initView();
        getCode();
    }

    private void initView() {
        phone = getIntent().getStringExtra("module_phone");
        binding.ivModuleClose.setOnClickListener(this);
        binding.tvModuleOk.setOnClickListener(this);
        binding.tvModuleSendCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivModuleClose:
                onBackPressed();
                break;
            case R.id.tvModuleSendCode:
                getCode();
                break;
            case R.id.tvModuleOk:
                code = binding.vcvModule.getEditContent();
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showToast("请输入验证码");
                    return;
                }

                changeMobile(code);
                break;
        }
    }

    private void getCode() {
        HTTPAPI.getInstance().getLoginCode(phone, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(UserInfoCodeActivity.this, s);
                } else {
                    timeUtil = new GetCodeTimeUtil(60000, 1000, binding.tvModuleSendCode);
                    timeUtil.start();
                }
            }
        });
    }

    private void changeMobile(String code) {
        HTTPAPI.getInstance().changeMobile(phone, code, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(UserInfoCodeActivity.this, s);
                } else {
                    ToastUtil.showToast("手机号码修改成功！");
                    SpUtils.clear(UserInfoCodeActivity.this);
                    startActivity(new Intent(UserInfoCodeActivity.this, LoginActivity.class));
                    ActivityStack.getScreenManager().popAllActivityExceptOne(LoginActivity.class);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeUtil != null) {
            timeUtil.onCancel();
        }
    }

}
